﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class UserControl_ControlPhanBoChiTieuBanHang : System.Web.UI.UserControl
{
    
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
       
        
    }
    void LoadGridPivot()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getPivotPhanBoTDV", CommandType.StoredProcedure,
              new SqlParameter("@salesp_key", ViewState["TDV"].ToString()),
              new SqlParameter("@Quy",ViewState["Quy"].ToString()));
            if (db.Rows.Count > 0)
            {

                ASPxPivotGrid1.DataSource = db;

            }
            else
            {

                ASPxPivotGrid1.DataSource = null;
            }
            ASPxPivotGrid1.DataBind();
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadGridTDV()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_getTDVForPhanBo]", CommandType.StoredProcedure,
                new SqlParameter("@Quy",drlistQuy.SelectedValue.ToString()),
                new SqlParameter("@Adncode", Session["Adncode"].ToString()));
            if (db.Rows.Count > 1)
            {

                grvTDV.DataSource = db;

            }
            else
            {
                grvTDV.DataSource = null;
            }
            ViewState["Quy"] = drlistQuy.SelectedValue.ToString();
            grvTDV.DataBind();
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void BtnShow_Click(object sender, EventArgs e)
    {
        LoadGridTDV();
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void grvTDV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;

                Label lblMaSo = (Label)grv.FindControl("lblsalesp_key");                
                Label lblHoTen = (Label)grv.FindControl("lblsalesp_name");
                CheckBox chIsSet = (CheckBox)grv.FindControl("chIsSet");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getSpSettedForTDV", CommandType.StoredProcedure,
                    new SqlParameter("@salesp_key", lblMaSo.Text),
                    new SqlParameter("@isSet", chIsSet.Checked),
                    new SqlParameter("@Quy",ViewState["Quy"].ToString()));

                if (db.Rows.Count > 1)
                {
                    TabContainer1.ActiveTabIndex = 1;
                    lblInforTDV.Text = lblMaSo.Text + ":" + lblHoTen.Text;
                    ViewState["TDV"] = lblMaSo.Text;
                    grvProduct.DataSource = db;
                    grvProduct.DataBind();
                }
                else if (db.Rows.Count == 1)
                {
                    if (db.Rows[0][0].ToString() == "Not")
                    {
                        ShowMessageBox("Bạn chưa phân tuyến cho TDV này");
                    }
                    else
                    {
                        TabContainer1.ActiveTabIndex = 1;
                        lblInforTDV.Text = lblMaSo.Text + ":" + lblHoTen.Text;
                        ViewState["TDV"] = lblMaSo.Text;
                        grvProduct.DataSource = db;
                        grvProduct.DataBind();
                    }
                }
                else
                {
                    ShowMessageBox("Bạn chưa phân tuyến cho TDV này");
                }

                
            }
          
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void grvTDV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvTDV.PageIndex = e.NewPageIndex;
        LoadGridTDV();

    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        //Create New
        ViewState["TDV"] = "";
        lblInforTDV.Text = "";
        ViewState["Quy"] = "";
        TabContainer1.ActiveTabIndex = 0;

    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

        dt = new ClassExecute.ClassExecute();
        try
        {
            if (grvProduct.Rows.Count > 0)
            {
                int kq = dt.ExecuteSQl_int(str, "k_deletePhanBoChiTieuTDV", CommandType.StoredProcedure, ref error,
                 new SqlParameter("@Quy", ViewState["Quy"].ToString()),
                 new SqlParameter("@salesp_key", ViewState["TDV"].ToString()));
                if (kq == 1)
                {
                   
                    ViewState["TDV"] = "";
                    lblInforTDV.Text = "";
                    ViewState["Quy"] = "";
                    TabContainer1.ActiveTabIndex = 0;
                    ShowMessageBox("Bạn đã xóa chỉ tiêu TDV thành công");
                }
                else if (kq == 2)
                {
                    ShowMessageBox("Bạn chưa chọn TDV");
                }
            }
            else
            {
                ShowMessageBox("Bạn chưa chọn TDV hoặc TDV này chưa được phân bổ sản phẩm");
            }
           
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void LinkGetHistorySetup_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
           

           
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (grvProduct.Rows.Count > 0)
            {
                int y = 1;
                Label Itemkey, NGUYENGIA;
                TextBox Qty;
                CheckBox IsFocus;
                float tongtien = 0;
                float Total = 0;
                bool kq;
                for (int i = 0; i < grvProduct.Rows.Count; i++)
                {
                    Qty = (TextBox)grvProduct.Rows[i].FindControl("txtSL");
                    NGUYENGIA = (Label)grvProduct.Rows[i].FindControl("lblCost");
                    Itemkey = (Label)grvProduct.Rows[i].FindControl("lblItemkey");
                    IsFocus = (CheckBox)grvProduct.Rows[i].FindControl("chIsFocus");

                    kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_savePhanBoTDV", ref error,
                               new SqlParameter("@salesp_key", ViewState["TDV"].ToString()),
                               new SqlParameter("@Quy", ViewState["Quy"].ToString()),
                               new SqlParameter("@Itemkey", Itemkey.Text),
                               new SqlParameter("@isFocus", IsFocus.Checked),
                               new SqlParameter("@QTY", float.Parse(Qty.Text)),
                               new SqlParameter("@NGUYENGIA", float.Parse(NGUYENGIA.Text)),
                               new SqlParameter("@Username", Session["user"].ToString()),
                               new SqlParameter("@i", y));
                    y++;
                    tongtien = tongtien + (float.Parse(Qty.Text) * float.Parse(NGUYENGIA.Text));
                    Total = Total + float.Parse(Qty.Text);

                }

                kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_savePhanBoTDVMaster", ref error,
                    new SqlParameter("@salesp_key", ViewState["TDV"].ToString()),
                    new SqlParameter("@Quy", ViewState["Quy"].ToString()),
                    new SqlParameter("@tongtien", tongtien),
                    new SqlParameter("@Username", Session["user"].ToString()),
                    new SqlParameter("@Total", Total));


                LoadGridTDV();
                LoadGridPivot();
                TabContainer1.ActiveTabIndex = 2;
                ShowMessageBox("Bạn đã phân bổ chỉ tiêu cho TDV:" + lblInforTDV.Text + " thành công");
            }
            else
            {
                ShowMessageBox("Không thể lưu vì TDV này chưa được phân bổ sản phẩm");
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
}
